<?php
class User extends MY_Controller {
	protected $_controller_path = '/user/User';
	protected $_list_path = '/user/list';
	protected $_model = 'user/User_Model';
	protected $_create_path = 'user/create';
	protected $_view_path = 'user/view';
	protected $_edit_path = 'user/edit';
	
	protected $_parent_model = 'role/Role_model';
	protected $_parent_id = 'role_id';
	protected $_is_search_parent = TRUE;
	protected $_resource_id = self::USER_RESOURCE;
	

	protected function setFormValidation() {
		$this -> form_validation -> set_rules('username', 'Username', 'trim|required');
		$this -> form_validation -> set_rules('password', 'Password', 'trim|required');
		$this -> form_validation -> set_rules('first_name', 'First name', 'trim');
		$this -> form_validation -> set_rules('last_name', 'Last name', 'trim');
		$this -> form_validation -> set_rules('email', 'Email', 'valid_email');
	}

	protected function init_input_data() {
		$username = $this -> input -> post('username');
		$password = $this -> input -> post('password');
		$first_name = $this -> input -> post('first_name');
		$last_name = $this -> input -> post('last_name');
		$email = $this -> input -> post('email');
		$role_id = $this -> input -> post('role_id');
		$meta_data = unsign($username . self::META_SEPERATE . $first_name . self::META_SEPERATE . $last_name . self::META_SEPERATE . $email);
		return array('username' => $username, 
					'password' => $password, 
					'first_name' => $first_name, 
					'last_name' => $last_name, 
					'email' => $email, 
					'role_id' => $role_id,
					'meta_data' => $meta_data, 
					);
	}


	protected function init_quick_input_data($id) {
		$username = $this -> input -> post('username'.$id);
		$password = $this -> input -> post('password'.$id);
		$first_name = $this -> input -> post('first_name'.$id);
		$last_name = $this -> input -> post('last_name'.$id);
		$email = $this -> input -> post('email'.$id);
		$meta_data = unsign($username . self::META_SEPERATE . $first_name . self::META_SEPERATE . $last_name . self::META_SEPERATE . $email);
		return array('username' => $username, 
					'password' => $password, 
					'first_name' => $first_name, 
					'last_name' => $last_name, 
					'email' => $email, 
					'meta_data' => $meta_data, 
					);
	}
	
	protected function setQuickEditFormValidation($id) {
		$this -> form_validation -> set_rules('username'.$id, 'Username', 'trim|required');
	}
	
	private function send_email($email) {
		$this->load->library('email');
		$this->email->from('hoanglt705@gmail.com', 'HoangLe');
		$this->email->to($email); 

		$this->email->subject('Email Test');
		$this->email->message('Testing the email class.');	

		$this->email->send();
		echo $this->email->print_debugger();
	}
	//TODO: improve performance
	protected function get_parent($id) {
		$this -> load -> model($this->_parent_model, 'parent_model');
		$data['result'] = $this -> model -> get($id);
		return $this -> parent_model -> get($data['result'] -> role_id);
	}
	
	// public static function register_resource() {
		// $resource = array(
			// 'id' => 5,
			// 'name' => 'User',
			// 'description' =>''
		// );
	// }
}
